﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This will set a pixel to foregroundcolor if it has more than m_nMaxCount neighbours with a greater value
    /// it will be set to backgroundcolor if it has less then m_nMaxCount neighbours with a greater value
    /// m_nMaxCount will be calculated by a quadratic window with side length n
    /// </summary>
    public class EnvironmentThreshold : BaseForeBackgroundColorFilter
    {
        protected int m_nMaxCount = 7;
        protected Color[] m_aColor = new Color[10];

        /// <summary>
        /// Initializes a new instance of the <see cref="EnvironmentThreshold"/> class.
        /// </summary>
        public EnvironmentThreshold()
        {
            M_PROCESSING_MODE = PROCESSING_MODE.ColorConstancy;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EnvironmentThreshold"/> class.
        /// </summary>
        /// <param name="_environmentThreshold">The _environment threshold.</param>
        internal EnvironmentThreshold(EnvironmentThreshold _environmentThreshold)
        {
            m_nMaxCount = _environmentThreshold.m_nMaxCount;
        }

        /// <summary>
        /// Gets or sets the max count.
        /// </summary>
        /// <value>The max count.</value>
        public int MaxCount
        {
            get { return m_nMaxCount; }
            set { m_nMaxCount = value; }
        }

        /// <summary>
        /// if a Bitmap is specified to this filter, use this execution overload
        /// </summary>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            UnsafeBitmap bitmap = _bitmap;
            int nHeight = bitmapCloned.Height;
            int nWidth = bitmapCloned.Width;

            ColorCalculation colorCalculation = Definitions.ColorCalculationDictionaryProcessingMode[M_PROCESSING_MODE];

            int x = 0;
            int y = 0;

            Color[] aColor = new Color[8];

            Color color = Color.Transparent;
            int nCount = 0;

            bitmapCloned.GetPixels();

            List<Point> definitionsEnvironmentPointListClockwise = Definitions.EnvironmentPointListClockwise;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmapCloned.GetPixel(x, y);
                    aColor = Segmentation.GetEnvironmentPixelColorArray(bitmapCloned, x, y, definitionsEnvironmentPointListClockwise, aColor);
                    nCount = 0;
                    foreach (Color colorTemp in aColor)
                    {
                        if (colorCalculation(colorTemp) < colorCalculation(color))
                        {
                            nCount++;
                        }
                    }
                    bitmap.SetPixel(x, y, nCount >= m_nMaxCount ? m_ColorForeground : m_ColorBackground);
                }
            }
            return _bitmap;
        }
        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EnvironmentThreshold(this);
        }
    }
}
